package com.lz.old.mixed;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
 * 遍历HashMap方法
 *
 * @author: Administrator
 * @date: 2018/06/24
 */
public class ForHashMap {

    private static String xx = "123";

    public static void Fun() {
        xx = "456";

    }

    Map m = new HashMap();
    List llll = new LinkedList();
    Set m1 = new HashSet<>();

    public static void main(String[] args) {
        System.out.println(xx);
        Fun();
        System.out.println(xx);
        xx = "789";
        System.out.println(xx);
        // map 遍历
        Map<String, String> m = new HashMap<String, String>();
        m.put("a", "1");
        m.put("b", "2");
        // 第一种，两次取值
        Set entrySet = m.entrySet();
        Set keySet = m.keySet();
        for (Object key : keySet) {
            System.out.println(key + "" + m.get(key));
        }
        // 第二种
        Iterator iterator = m.entrySet().iterator();
        while (iterator.hasNext()) {
            Entry map = (Entry) iterator.next();
            System.out.println(map.getKey());
            System.out.println(map.getValue());
        }

        // 第三种 只能遍历值
        for (Object entry : m.values()) {
            System.err.println(entry);
        }
        Set hSet = new HashSet<>();
        hSet.add("a1");
        hSet.add("b1");
        hSet.add("c1");
        for (Object value : hSet) {

            System.out.println(value);

        }

        int i = 5;
        Integer ii = 10;
        // 自动装箱
        Integer j = new Integer(i);
        // 自动拆箱
        int jj = ii.intValue();
        MyThread x = new MyThread();
        Thread t1 = new Thread(x, "1");
        Thread t2 = new Thread(x, "2");
        t2.start();
        t1.start();
    }

}

// 多线程买票
class MyThread implements Runnable {
    private int num = 0; // 出票数
    private int count = 1000; // 剩余票数

    boolean flag = false;

    @Override
    public void run() {
        while (true) {
            synchronized (this) {
                // 没有余票时，跳出循环
                if (count <= 0) {
                    break;
                }
                num++;
                count--;

                try {
                    Thread.sleep(10);// 模拟网络延时
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("显示出票信息：" + Thread.currentThread().getName()
                        + "抢到第" + num + "张票，剩余" + count + "张票");
            }
        }

    }
}
